import java.util.ArrayList;
import java.util.Collections;

/**
 Problem 4
 16 November 2001

 A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91  99.

 Find the largest palindrome made from the product of two 3-digit numbers.
 */

/**
 * @author Hitec
 * 
 */
public class Program {
	
	private static final int START = 100;
	private static final int COUNT = 900;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ArrayList<Integer> products = new ArrayList<Integer>();

		for (int x : Enumerable.Range(START, COUNT)) {
			for (int y : Enumerable.Range(START, COUNT)) {
				if (x <= y) {
					if (isPalindrome(x * y))
						products.add(x * y);
				}
			}
		}
		
		Collections.sort(products, new ProductComparer());
		
		System.out.println(products.get(products.size() - 1));
	}

	private static boolean isPalindrome(long product) {
		String source = Long.toString(product);
		
		return source.equalsIgnoreCase(reverseString(source));
	}

	public static String reverseString(String source) {
		int i, len = source.length();
		StringBuffer dest = new StringBuffer(len);

		for (i = (len - 1); i >= 0; i--)
			dest.append(source.charAt(i));
		return dest.toString();
	}
}
